package com.wzq.permission.center.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wzq.permission.center.mapper.OrganizeMapper;
import com.wzq.permission.center.mapper.OrganizeRoleMapper;
import com.wzq.permission.center.mapper.UserOrganizeMapper;
import com.wzq.permission.client.model.Pager;
import com.wzq.permission.client.model.admin.OrganizeModel;
import com.wzq.permission.client.model.admin.OrganizeRoleModel;
import com.wzq.permission.client.model.admin.UserOrganizeModel;
import com.wzq.permission.client.service.OrganizeService;

/**
 * 组织服务
 */
@Service
@Transactional
public class OrganizeServiceImpl implements OrganizeService {
	@Autowired
    private OrganizeMapper mapper;
	@Autowired
	private UserOrganizeMapper userOrgMapper;
	@Autowired
	private OrganizeRoleMapper orgRoleMapper;
	
	@Override
	public void add(OrganizeModel organize) {
		mapper.add(organize);
	}

	@Override
	public void update(OrganizeModel organize) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateBySelective(OrganizeModel organize) {
		mapper.updateBySelective(organize);
		
	}

	@Override
	public List<OrganizeModel> queryList(OrganizeModel organize, Pager pager) {
		return null;
	}

	@Override
	public OrganizeModel queryById(Object id) {
		return mapper.queryById(id);
	}

	@Override
	public void delete(Long[] organizeIds) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * 查询菜单列表
	 * @return
	 */
	@Override
	public List<OrganizeModel> queryMenuTree(){
		List<OrganizeModel> orgList = mapper.queryMenuTree();
		return orgList;
	}

	@Override
	public void addUser(List<UserOrganizeModel> userOrgList) {
		if(userOrgList != null && userOrgList.size() > 0){
			userOrgMapper.deleteByOrgId(userOrgList.get(0).getOrganizeId());
			userOrgMapper.addBatch(userOrgList);
		}
	}

	@Override
	public UserOrganizeModel queryUserOrgById(Object id) {
		return userOrgMapper.queryUserOrgById(id);
	}

	@Override
	public List<UserOrganizeModel> queryUserOrgList(UserOrganizeModel userOrganize, Pager pager) {
		return userOrgMapper.queryUserOrgList(userOrganize,pager);
	}

	@Override
	public int queryUserOrgByCount(UserOrganizeModel userOrganize) {
		return userOrgMapper.queryByCount(userOrganize);
	}

	@Override
	public void deleteUser(Long[] userOrganizeIds) {
		userOrgMapper.deleteUser(userOrganizeIds);
	}

	@Override
	public void deleteOrganize(Long organizeId) {
		mapper.logicDelete(organizeId);
		userOrgMapper.deleteByOrgId(organizeId);
		orgRoleMapper.deleteByOrgId(organizeId);
	}
	
	
	/**
	 * 查询菜单列表
	 * @return
	 */
	@Override
	public List<OrganizeRoleModel> queryRoleTree(){
		List<OrganizeRoleModel> orgRoleList = orgRoleMapper.queryRoleTree();
		return orgRoleList;
	}

	@Override
	public List<String> getRoleIdByOrgId(Object organizeId) {
		List<String> roleIdList = orgRoleMapper.getRoleIdByOrgId(organizeId);
		return roleIdList;
	}

	@Override
	public void distributionSave(Long organizeId,List<OrganizeRoleModel> orgRoleList) {
	    orgRoleMapper.deleteByOrgId(organizeId);
		if(orgRoleList != null && orgRoleList.size() > 0){
			orgRoleMapper.addBatch(orgRoleList);
		}
	}

    @Override
    public int isExist(OrganizeModel organize) {
        return mapper.isExist(organize);
    }
	
	
}
